package com.wise.service.local.content.menutype;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.wise.entities.content.Menu;
import com.wise.framework.exception.Excep;
import com.wise.framework.validation.Precondition;
import com.wise.summer.v1.local.content.menutype.ProductMenuTypeValidation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.util.*;

import static com.wise.model.content.ContentErrMsgs.Err_000500000023;

/**
 * @author shaoming
 * @date 2019-09-09
 */
@Service
@SummerService
public class ProductMenuTypeValidationService implements SummerServiceBean<ProductMenuTypeValidation> {
	@Override
	public void sum(ProductMenuTypeValidation summer) {
		Precondition.checkState(StringUtils.isNotBlank(summer.getExtContent()), Err_000500000023);
		List<Menu.ExtContent> ext;
		try {
			ext = JSON.parseArray(summer.getExtContent(), Menu.ExtContent.class);

		} catch (Exception ignored) {
			throw Excep.le(this.getClass(), Err_000500000023);
		}
		ext.forEach(it -> {
			Precondition.validation(it);
			if (CollectionUtil.isNotEmpty(it.getInfo())) {
				it.getInfo().forEach(Precondition::validation);
			}
		});
		summer.setSummerResult(ext);
	}
}
